////////////////////////////////////////////////////////////////////////////////
//  COPYRIGHT (C) 2010 TELECOM DIGITAL MEDIA ("TDMEDIA").
//  ALL RIGHTS RESERVED.
//
//  THIS IS CONFIDENTIAL AND PROPRIETARY INTELLECTUAL PROPERTY OWNED BY AND
//  CREATED ON BEHALF OF TDMEDIA. ANY FORM OF DISTRIBUTION, COPY,
//  MODIFICATION WITHOUT THE WRITTEN CONSENT FROM TDMEDIA IS STRICTLY
//  PROHIBITED.
////////////////////////////////////////////////////////////////////////////////
package cc.telecomdigital.tdstock.activity.groups.more;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import cc.telecomdigital.tdstock.CommonDefn;
import cc.telecomdigital.tdstock.ITDLApplication;
import cc.telecomdigital.tdstock.R;
import cc.telecomdigital.tdstock.Framework.UniqueItem.TDStockLog;
import cc.telecomdigital.tdstock.Http.H_CODE;
import cc.telecomdigital.tdstock.Http.HttpSuperClassStockBaseActivity;
import cc.telecomdigital.tdstock.Http.Services.HttpAppInfo;
import cc.telecomdigital.tdstock.Http.Services.H_HttpServiceRequest.H_UserClient_HttpRequest_Result;
import cc.telecomdigital.tdstock.Tools.ActivityGroupController;
import cc.telecomdigital.tdstock.activity.groups.MoreGroup;
import cc.telecomdigital.tdstock.adapter.TwoTextColumnHolder;
import cc.telecomdigital.tdstock.adapter.TwoTextColumnItemAdapter;

/**
 * @author haibo for list item named "AH Comparison"
 */
public class InterestRateActivity extends HttpSuperClassStockBaseActivity
{
	protected static final String		TAG			= InterestRateActivity.class.getSimpleName();
	private static final long			START_ID	= 90000l;
	private TextView					_topBar;
	private ListView					_listView;
	private Button						_toolBtnReturn;
	private TwoTextColumnItemAdapter	_adapter;
	private List<TwoTextColumnHolder>	_list;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.more_interest_rate);

		_toolBtnReturn = (Button) findViewById(R.id.more_returnbtn);
		_toolBtnReturn.setFocusable(true);
		_toolBtnReturn.setClickable(true);
		_toolBtnReturn.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				TDStockLog.d(TAG, "close current activity and forwarding to the previous");

				Intent intent = new Intent(getApplicationContext(), FinanceListActivity.class);
				intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NO_USER_ACTION);
				SwitchForwardActivity(FinanceListActivity.class, intent);
			}
		});

		_topBar = (TextView) findViewById(R.id.top_bar_title);
		_listView = (ListView) findViewById(R.id.interest_rate_list);

		_list = new ArrayList<TwoTextColumnHolder>();

		_adapter = new TwoTextColumnItemAdapter(LayoutInflater.from(this), _list);

		_listView.setAdapter(_adapter);
	}

	@Override
	protected void onResume()
	{
		super.onResume();
		_listView.invalidateViews();		
	}

	private void updateAdapter(List<String[]> list)
	{
		_list.clear();
		if (list == null)
		{ // mock the empty row
			for (int index = 0; index < 100; index++)
			{
				TwoTextColumnHolder holder = new TwoTextColumnHolder();
				holder.left = new TextView(this);
				holder.right = new TextView(this);
				holder.id = index;
				_list.add(holder);
			}
			_adapter.notifyDataSetChanged();
			return;
		}
		for (String[] array : list)
		{
			TwoTextColumnHolder holder = new TwoTextColumnHolder();
			holder.left = new TextView(this);
			holder.right = new TextView(this);
			holder.id = START_ID + _list.size();
			holder.left.setText(array[0] == null ? CommonDefn.DEFAULT_FIELD_VALUE : array[0]);
			holder.right.setText(array[1] == null ? CommonDefn.DEFAULT_FIELD_VALUE : array[1]);
			_list.add(holder);
		}
		((ITDLApplication) getApplicationContext()).setObject(null);
		_adapter.notifyDataSetChanged();
	}

	private void updateTitle()
	{
		String title = ((ITDLApplication) getApplicationContext()).getString(CommonDefn.TOP_BAR_TITLE);
		if (_topBar != null && title != null)
			_topBar.setText(title);
	}

	@Override
	public ActivityGroupController GetActivityGroupController()
	{
		return MoreGroup.GetActivityGroupHostControl();
	}

	@Override
	protected H_CODE GetDataFromHttpServer()
	{
		return HttpAppInfo.SendFinanceRequest(actContext, 3, this);
	}

	@Override
	protected void H_UserClient_HttpRequest_Response_Sub(final H_UserClient_HttpRequest_Result iResult)
	{
		if (!iResult.MStatus.equals("0"))
		{
			ShowMessageDialog(iResult.MError + "(" + iResult.MStatus + ")");
			return;
		}
		
		TDStockLog.i(TAG, "Count: " + iResult.contentStores.size());
		for (String[] s : iResult.contentStores)
		{
			TDStockLog.i(TAG, s[0]);
			if(s.length>1)
				TDStockLog.i(TAG, s[1]);
			TDStockLog.d(TAG, "------------------------------------");
		}
		
		Runnable runnable = new Runnable()
		{
			@Override
			public void run()
			{
				updateTitle();
				updateAdapter(iResult.contentStores);
			}
		};
		_messageHandler.post(runnable);
	}

}
